import {computed} from "vue";
import {mapState, useStore} from 'vuex'

/**
 * 获取state数据
 * @param mapper 数组或对象
 * @returns {{}} 对象，建议解构，得到的是ref包装过的数据
 */
export function useState(mapper) {
    // 得到store
    const store = useStore()
    // 获取到对应的对象的functions
    const storeStateFns = mapState(mapper)
    // 对数据进行转换
    const storeState = {}
    Object.keys(storeStateFns).forEach(fnKey => {
        const fn = storeStateFns[fnKey].bind({$store: store})
        storeState[fnKey] = computed(fn)
    })

    return storeState
}
